package cn.iocoder.yudao.module.locale.controller.admin.tabledata;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.locale.controller.admin.tabledata.vo.*;
import cn.iocoder.yudao.module.locale.dal.dataobject.tabledata.TableDataDO;
import cn.iocoder.yudao.module.locale.service.tabledata.TableDataService;

@Tag(name = "管理后台 - 表数据国际化")
@RestController
@RequestMapping("/locale/table-data")
@Validated
public class TableDataController {

    @Resource
    private TableDataService tableDataService;

    @PostMapping("/create")
    @Operation(summary = "创建表数据国际化")
    @PreAuthorize("@ss.hasPermission('locale:table-data:create')")
    public CommonResult<Long> createTableData(@Valid @RequestBody TableDataSaveReqVO createReqVO) {
        return success(tableDataService.createTableData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新表数据国际化")
    @PreAuthorize("@ss.hasPermission('locale:table-data:update')")
    public CommonResult<Boolean> updateTableData(@Valid @RequestBody TableDataSaveReqVO updateReqVO) {
        tableDataService.updateTableData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除表数据国际化")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('locale:table-data:delete')")
    public CommonResult<Boolean> deleteTableData(@RequestParam("id") Long id) {
        tableDataService.deleteTableData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得表数据国际化")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('locale:table-data:query')")
    public CommonResult<TableDataRespVO> getTableData(@RequestParam("id") Long id) {
        TableDataDO tableData = tableDataService.getTableData(id);
        return success(BeanUtils.toBean(tableData, TableDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得表数据国际化分页")
    @PreAuthorize("@ss.hasPermission('locale:table-data:query')")
    public CommonResult<PageResult<TableDataRespVO>> getTableDataPage(@Valid TableDataPageReqVO pageReqVO) {
        PageResult<TableDataDO> pageResult = tableDataService.getTableDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TableDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出表数据国际化 Excel")
    @PreAuthorize("@ss.hasPermission('locale:table-data:export')")
    @OperateLog(type = EXPORT)
    public void exportTableDataExcel(@Valid TableDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TableDataDO> list = tableDataService.getTableDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "表数据国际化.xls", "数据", TableDataRespVO.class,
                        BeanUtils.toBean(list, TableDataRespVO.class));
    }

}